<template>
  <div>
    <h1>宿舍信息管理</h1>
    <el-input v-model="name" placeholder="请输入名称"></el-input>
    <el-button type="primary" @click="findPage">查询</el-button>
    <el-button type="primary" @click="toAdd">添加</el-button>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID">
      </el-table-column>
      <el-table-column
          prop="name"
          label="宿舍名">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="toUpdate(scope.row)" type="text" size="small">修改</el-button>
          <el-button @click="toDel(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2,4,6,8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog :title="form.id==null?'添加宿舍':'修改宿舍'" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="宿舍名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="form={};dialogFormVisible = false;">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "RoomView",
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 2,
      total: 0,
      name: '',
      dialogFormVisible: false,
      form: {}
    }
  },
  created() {
    this.findPage()
  },
  methods: {
    toDel(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get("/room/del?id=" + row.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.findPage();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    toUpdate(row) {
      // 修改和添加，用的一个弹出框
      this.dialogFormVisible = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    add() {
      this.axios.post("/room/add", this.form).then(res => {
        this.dialogFormVisible = false;
        this.findPage();
        // 清空数据
        this.form = {};
      })
    },
    toAdd() {
      this.dialogFormVisible = true;
    },
    findPage() {
      this.axios.post('/room/findPage?pn=' + this.pageNum + '&ps=' + this.pageSize + '&name=' + this.name).then(res => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.findPage();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.findPage();
    }
  }
}
</script>


<style>

</style>